package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.lang.Nullable;

import java.io.IOException;

/**
 * @ClassName FastDFSClient
 * @Description 操作文件上传的工具类
 * @Author 传智播客
 * @Date 11:02 2019/8/12
 * @Version 2.1
 **/
public class FastDFSClient {

    /**
     * @author 栗子
     * @Description 初始化FastDFS的配置文件
     * @Date 11:05 2019/8/12
     * @return
     **/
    static {
        String config_name = new ClassPathResource("fdfs_client.conf").getPath();
        try {
            // 初始化FastDFS的配置文件
            ClientGlobal.init(config_name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author 栗子
     * @Description 附件上传
     * @Date 11:12 2019/8/12
     * @param fastDFSFile
     * @return java.lang.String[]
     **/
    /*
    public static String[] uploadFile(FastDFSFile fastDFSFile){

        try {
            byte[] file_buff = fastDFSFile.getContent();
            String file_ext_name = fastDFSFile.getExt();
            NameValuePair[] meta_list = new NameValuePair[1];
            meta_list[0] = new NameValuePair(fastDFSFile.getAuthor());
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、将附件上传
            String[] uploadResult = storageClient.upload_file(file_buff, file_ext_name, meta_list);
            return uploadResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    */

    /**
     * @author 栗子
     * @Description
     * @Date 11:49 2019/8/12
     * @param file_buff 文件内容
     * @param file_ext_name 文件扩展名
     * @param des  文件描述
     * @return java.lang.String[]
     **/
    public static String[] uploadFile(byte[] file_buff, String file_ext_name, @Nullable String des){

        try {
            NameValuePair[] meta_list = new NameValuePair[1];
            meta_list[0] = new NameValuePair(des);
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、将附件上传
            String[] uploadResult = storageClient.upload_file(file_buff, file_ext_name, meta_list);
            return uploadResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 文件下载
     * @Date 11:55 2019/8/12
     * @param group_name 组名
     * @param remote_filename 服务器上的文件名称
     * @return byte[]
     **/
    public static byte[] downloadFile(String group_name, String remote_filename){
        try {
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、文件下载
            byte[] bytes = storageClient.download_file(group_name, remote_filename);
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 附件删除
     * @Date 12:00 2019/8/12
     * @param group_name
     * @param remote_filename
     * @return void
     **/
    public static void deleteFile(String group_name, String remote_filename){
        try {
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、文件删除
            storageClient.delete_file(group_name, remote_filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author 栗子
     * @Description 获取附件信息
     * @Date 12:09 2019/8/12
     * @param group_name
     * @param remote_filename
     * @return org.csource.fastdfs.FileInfo
     **/
    public static FileInfo getFileInfo(String group_name, String remote_filename){
        try {
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、文件删除
            FileInfo fileInfo = storageClient.get_file_info(group_name, remote_filename);
            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 获取存储服务器信息
     * @Date 12:17 2019/8/12
     * @param groupName 组名
     * @return org.csource.fastdfs.StorageServer
     **/
    public static StorageServer getStoreStorage(String groupName){
        try {
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 获取存储服务器信息
            StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer, groupName);
            return storeStorage;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 获取多个存储服务器
     * @Date 12:23 2019/8/12
     * @param groupName
     * @param filename
     * @return org.csource.fastdfs.ServerInfo[]
     **/
    public static ServerInfo[] getServerInfos(String groupName, String filename){
        try {
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 获取存储服务器信息
            ServerInfo[] serverInfos = trackerClient.getFetchStorages(trackerServer, groupName, filename);
            return serverInfos;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 获取服务器地址
     * @Date 11:23 2019/8/12
     * @param
     * @return java.lang.String
     **/
    public static String getServerUrl(){
        try {
            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器client获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、拼接服务器地址
            String hostAddress = trackerServer.getInetSocketAddress().getAddress().getHostAddress();
            int port = ClientGlobal.getG_tracker_http_port();
            String url = "http://" + hostAddress + ":" + port;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
